package local.common.listener;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.tiles.TilesApplicationContext;
import org.apache.tiles.TilesContainer;
import org.apache.tiles.context.TilesRequestContextFactory;
import org.apache.tiles.definition.DefinitionsFactoryException;
import org.apache.tiles.evaluator.AttributeEvaluatorFactory;
import org.apache.tiles.factory.BasicTilesContainerFactory;
import org.apache.tiles.freemarker.renderer.FreeMarkerAttributeRenderer;
import org.apache.tiles.renderer.impl.BasicRendererFactory;

public class TestTilesContainerFactory extends BasicTilesContainerFactory {

    @Override
    protected List<URL> getSourceURLs(TilesApplicationContext applicationContext,
            TilesRequestContextFactory contextFactory) {
        List<URL> urls = new ArrayList<URL>();
        try {
            urls.add(applicationContext.getResource("/WEB-INF/tiles.xml"));
        } catch (IOException e) {
            throw new DefinitionsFactoryException("Cannot load definition URLs", e);
        }
        return urls;
    }
    
    @Override
    protected void registerAttributeRenderers(
    		BasicRendererFactory rendererFactory,
    		TilesApplicationContext applicationContext,
    		TilesRequestContextFactory contextFactory,
    		TilesContainer container,
    		AttributeEvaluatorFactory attributeEvaluatorFactory) {
    	super.registerAttributeRenderers(rendererFactory, applicationContext,
    			contextFactory, container, attributeEvaluatorFactory);
    	
		FreeMarkerAttributeRenderer freemarkerRenderer = new FreeMarkerAttributeRenderer();
	    freemarkerRenderer.setApplicationContext(applicationContext);
	    freemarkerRenderer.setAttributeEvaluatorFactory(attributeEvaluatorFactory);
	    freemarkerRenderer.setRequestContextFactory(contextFactory);
	    freemarkerRenderer.setParameter("TemplatePath", "/");
	    freemarkerRenderer.setParameter("NoCache", "true");
	    freemarkerRenderer.setParameter("ContentType", "text/html");
	    freemarkerRenderer.setParameter("template_update_delay", "0");
	    freemarkerRenderer.setParameter("default_encoding", "UTF-8");
	    freemarkerRenderer.setParameter("number_format", "0.##########");
	    freemarkerRenderer.commit();
	    rendererFactory.registerRenderer("freemarker", freemarkerRenderer);
    }
    
}
